using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;
using CMS.Web;
using ProjectBase.Utils;

namespace CMS.Web.ListManager
{
    public partial class AddDiaBan : BaseUserControl, IAddDiaBanView
    {
        private AddDiaBanPresenter presenter;
        private DiaBan DiaBanLookup;
        string maDiaBan = "";

        public AddDiaBan()
        {           
            this.presenter = new AddDiaBanPresenter(this, NHibernateDaoFactory.Instance.GetDiaBanDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            maDiaBan = CRequest.GetQueryUri("MaDiaBan", "0");
            if (!maDiaBan.Equals("0"))
            {
                this.DiaBanLookup = new DiaBanDao().GetById(Convert.ToInt32(maDiaBan));
            }
            else
            {
                this.trDiaBanLookup.Visible = false;
            }
            // For Ajax
            this.RaiseClientEvent();
            if (!this.IsPostBack)
            {
                if (!maDiaBan.Equals("0"))
                {
                    this.txtSTT.Text = Convert.ToString(presenter.GetMaxSTT(this.DiaBanLookup.ID));
                    this.lblDiaBanLookup.Text = this.DiaBanLookup.TenDiaBan;
                }
            }
            this.btnOK.Attributes.Add("onclick", "return CheckInput(0);");
            this.btnOKC.Attributes.Add("onclick", "return CheckInput(1);");
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddLinhVucPresenter));
        }        

        protected void btnOK_Click(object sender, EventArgs e)
        {
            DiaBan diaban = presenter.AddObj(Page.IsValid);
            Response.Redirect(BaseUrl + "&action=VIEW&MaDiaBan=" + diaban.ID);           
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            DiaBan diaban = presenter.AddObj(Page.IsValid);
            this.maDiaBan = diaban.ID.ToString();
            this.txtName.Text = "";
            this.txtName.Focus();
            if (this.DiaBanLookup != null)
            {
                this.txtSTT.Text = Convert.ToString(presenter.GetMaxSTT(this.DiaBanLookup.ID));
            }
            else
                this.txtSTT.Text = "1";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW&MaDiaBan=" + this.maDiaBan);
        }

        #region IAddDiaBanView Members

        public string Message
        {
            set
            { //lblMessage.Text = value; 
            }
        }

        public DiaBan DiaBanToAdd
        {
            get
            {
                DiaBan obj = new DiaBan();
                obj.TenDiaBan = txtName.Text.Trim();
                if ((txtSTT.Text.Trim() != null) && (!txtSTT.Text.Trim().Equals("")))
                {
                    obj.ThuTu = Convert.ToByte(txtSTT.Text.Trim());
                }
                obj.MaDiaBanTrucThuocLookup = this.DiaBanLookup;
                obj.TrangThai = Convert.ToBoolean(radStatus.SelectedValue);
                return obj;
            }
        }

        #endregion
    }
}